/*
 * ClcokCountdown.fx
 *          VisualClock Project
 *   http://code.google.com/p/visualclock/
 *   This app draws a visual clock counting backward
 *   See other files for different clock types
 *
 * Created on Nov 15, 2009
 *
 * v0.1a 111509 - initial creation (Scott Szretter)
 *
 *
 */

package visualclock;

import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.animation.*;
import java.util.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.util.Math;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.control.Label;
import java.text.SimpleDateFormat;

var hours;
var minutes;
var seconds=60;
var date;
var day;
var month;
var year;
var cal;
var strokeclr = Color.GREEN;

//function createCalendar() {
//    var date = new Date();
//    def calendar = Calendar.getInstance();
//    calendar.setTime(new Date());
//    calendar
//}

//var sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var today:String = "countdown";

var time = Timeline {
    repeatCount: Timeline.INDEFINITE
    keyFrames: [
        KeyFrame {
            time: 1s //count by 1 second
            action:function() {
                //cal = createCalendar();
                if(seconds==0) seconds=60;
                seconds--;
                //minutes = (cal.get(Calendar.MINUTE) + seconds)/60;
                //hours = (cal.get(Calendar.HOUR) + minutes)/12;
                today = "{seconds}";
                strokeclr = Color.BLACK; //set stroke color to black to erase
         }}]}





class OneLineCountdown extends CustomNode {
    public var cX=10;
    public var cY=10;
    public var strokeWidth = 5;
    public var radius=50;
    public var secLength= 180.0;
    public var strokeColor = Color.GREEN;
    //public var minLength= 90.0;
    //public var hourLength= 30.0;
    public var todayText ="";
    override function create() {
        return Group {
            opacity: 0.85
            content:[
                Arc {// second
                    centerX: cX, centerY: cY
                    radiusX: radius, radiusY: radius
                    startAngle: 90, length:bind -secLength+strokeWidth*360.0/(4*Math.PI*radius)
                    stroke: bind strokeColor;
                    fill: null
                    strokeWidth: strokeWidth
                    }
//                Arc {// minute
//                    centerX: cX, centerY: cY
//                    radiusX: radius-17, radiusY: radius -17
//                    startAngle: 90, length:bind -minLength+strokeWidth*360.0/(4*Math.PI*radius)
//                    stroke: Color.RED
//                    fill: null
//                    strokeWidth: strokeWidth
//                    }
//                Arc {// hour
//                    centerX: cX, centerY: cY
//                    radiusX: radius-34, radiusY: radius-34
//                    startAngle: 90, length:bind -hourLength+strokeWidth*360.0/(4*Math.PI*radius)
//                    stroke: Color.BLUE
//                    fill: null
//                    strokeWidth: strokeWidth
//                    }
                Label {
                    translateY : cY+radius + 10
                    translateX : cX - radius/2;
                    text: bind todayText;
                    textFill: Color.RED
                    }
            ]; //content
        }; //return group
    } // function create
} // class

public function run () {
    time.play();
//println("{today}");
    var scene:Scene = Scene {
        width: 300
        height: 340
        fill: Color.BLACK
        content: [
            //ImageView {   image: Image {
            //url: "{__DIR__}backgroundImage.png"
            //}}

          OneLineCountdown { // INSTANCE  -- static starting shape
                radius:120
                cX: 150
                cY: 160
                strokeWidth:15
                secLength:360*seconds
               // minLength: bind 360*minutes
               // hourLength:bind 360*hours
                //todayText: bind "{today}"
                strokeColor: Color.GREEN;
            }

            OneLineCountdown { // INSTANCE -- black (bg color) to erase
                radius:120
                cX: 150
                cY: 160
                strokeWidth:15
                secLength:bind 360*seconds
               // minLength: bind 360*minutes
               // hourLength:bind 360*hours
                todayText: bind "{today}"
                strokeColor: bind strokeclr;
            }


        ] //content
    } //scene

    def stage:Stage = Stage {
        title: "VisualClock"
        scene: scene
    } //stage

    stage.visible = true;
  
} //function run


